<template>
  <div class="">
    <!-- 内容组件 -->
    <content-template :formData="formData"
                      :searchData="searchData"
                      :tableData="tableData"
                      :getDatas ="getDatas"
                      @handleSubmit="handleSubmit">
            <div style="display:flex;align-items:center" slot="content_top_left_button">
                <i-switch  true-color="#13ce66" false-color="#ff4949" v-model="adswitch" @on-change="change" />
                <span style="margin-left:10px">{{adswitch?'显示':'不显示'}}</span>
            </div>  
    </content-template>
    <!-- 弹框组件 -->
    <dialog-template
            :options = "formData.options.hasAdd">
            <!-- 表单组件 -->
        <form-template
            :formData="formData"
            :options="formData.options.hasAdd"
            :formValidate="formData.formValidate"
            :rules="formData.ruleValidate"
            :list="formData.list"
            @handleSubmit="handleSubmit"
        >             
        </form-template>
    </dialog-template>
  </div>
</template>
<script>
export default {
   data() {
    return {
      adswitch:false,
      formData:{
        index:'',
        options:{
          hasAdd:{  //需不需要新增按钮——为空时不显示
            modal: false,        //弹框状态
            isedit: false,       //是否是编辑状态
            title:'签到顶部广告' ,      //弹框标题
            width:800,           //弹框宽度
            class:"dialogClass", //弹框类名
            zIndex:100,          //弹框层级
            loading:true,        //弹框加载圈

            formValidate: {    //获取的表单数据
              image_url: '', 
              sort: '',
              ad_url:''
            },
            ruleValidate: {    //验证表单信息
              sort: [
                { required: true, message: '请填写排序', trigger: 'blur' },
              ],
              ad_url: [
                { required: true, message: '请填写广告链接', trigger: 'blur' },
              ],
            },
          },
          hasSelect:{
            selectList:[
               
            ]
          }       
        },
        list: [ //表单列表——进行一些表单操作
          // {
          //    type: 表单类型,
          //    label: 表单标题，
          //    prop:表单下标指引，要对应formValidate的字段
          //    formStyle:自定义表单的样式
          //    value:传入自定义数据
          // }
          {
            type: "uploadImg",      
            prop: "image_url",
            label: "上传图片",
            formStyle:"width:200px;height:100px"
          },
          {
            type: "input",
            prop: "ad_url",
            label: "广告链接",
            placeholder:"广告链接",
            value:''
          },
          {
            type: "input",
            prop: "sort",
            label: "排序",
            placeholder:"排序",
            value:''
          },
        ],
      },
      searchData:{  
        searchValue: {  //搜索值
  
        },
        searchColumns: [ //搜索条件和搜索数量——不传则没有
          
        ]
      },
      tableData:{  //表格数据及操作
        columns: [
            {
                title: '排序',
                key: 'sort',
                width:100,
                align: 'center',
                sortable: true
            },
            {
              title: '轮播图',
              align: 'center',
              render: (h, params) => {
              if (params.row.image_url != '') {
                return h('img', {
                  style: {
                    padding:'10px',
                    width: '200px',
                    height: '100px'
                  },
                  attrs: {
                    src: this.$imgUrl + params.row.image_url
                  }
                })
              } else {
                return h('div', {
                  style: {
                    padding:'10px',
                    width: '200px',
                    height: '200px'
                  }
                })
              }
            }
            },
            {
                title: '广告链接',
                key: 'ad_url',
                align: 'center',
            },
            {
              title: '操作',
              key: 'action',
              width: 200,
              align: 'center',
              render: (h, params) => {
                return h('div', [
                  h('Button', {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.edit(params.index)
                      }
                    }
                  }, '修改'),
                  h('Button', {
                    props: {
                      type: 'error',
                      size: 'small'
                    },
                    on: {
                      click: () => {
                        this.deletes(params.index)
                      }
                    }
                  }, '删除'),
                ]);
              }
          }
        ],
        data: [], //表格数据
        total:0,  //表格数据总量
        page:1,   //表格数据页码
        per_page:10,  //表格一页显示几条数据
        loading: true, //表格数据预加载
      }
    }
  },
  mounted() {
    this.getDatas();
    this.getsever()
  },
  methods: {
    change (status) {
       this.handleChange()
    },
    getsever(){
      let params = {
         
      }
      this.$api.getseverdetail(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
               this.adswitch = res.data.is_sign_top_ad == 1 ? false:true
          }
      })
    },
    handleChange(value){
        let params = {
          is_sign_top_ad:this.adswitch?2:1,
        };
        this.$api.editsever(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$Message.success('设置成功');
              this.getsever();
            }
        })    
    },
    getDatas(){
      let params = {
        type:6,
        page:this.tableData.page,
        per_page:this.tableData.per_page,
      }
      this.$api.getbanner(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.tableData.data = res.data.data
              this.tableData.total = res.data.total
              this.tableData.loading = false
          }
      })
    },
    changeStatus(index){
      let status = "";
      let text = "";
      switch( this.tableData.data[index].status){
        case 1:
          text = "禁用"
          status = 2
          break;
        case 2:
          text = "启用"
          status = 1
          break;
        default:
          console.log('其他')
      };
      let params = {
        manager_id: this.tableData.data[index].manager_id,
        status: status
      }
      this.$api.statusbanner(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$Message.success( text + '成功');
              this.getDatas();
            }
      })
    },
    edit(index){
      this.formData.index = index;
      this.formData.options.hasAdd.formValidate = {
        image_url: this.tableData.data[index].image_url,
        sort: this.tableData.data[index].sort,
        ad_url:this.tableData.data[index].ad_url,
      };
      this.formData.options.hasAdd.isedit = true
      this.formData.options.hasAdd.modal = true
    },
    deletes(index){
      this.$Modal.confirm({
        title: '提示',
        content: '确认删除该签到顶部广告吗?',
        onOk: () => {
          let params = {
            rotation_chart_id : this.tableData.data[index].rotation_chart_id
          }
          this.$api.deletesbanner(params).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$Message.success('删除成功');
              this.getDatas();
            }
          })
        }
      })
    },
    handleSubmit(value){
      if ( this.formData.options.hasAdd.isedit == true) {
        let params = {
          type:6,
          rotation_chart_id :  this.tableData.data[this.formData.index].rotation_chart_id,
          image_url: this.formData.options.hasAdd.formValidate.image_url,
          sort:     this.formData.options.hasAdd.formValidate.sort,
          ad_url:this.formData.options.hasAdd.formValidate.ad_url,
        };
        this.$api.editbanner(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.formData.options.hasAdd.modal = false;
              this.$Message.success('修改成功');
              this.getDatas();
            }
        })
      }else{
        let params = {
          type:6,
          image_url: this.formData.options.hasAdd.formValidate.image_url,
          sort:     this.formData.options.hasAdd.formValidate.sort,
          ad_url:this.formData.options.hasAdd.formValidate.ad_url,
        }
        this.$api.createbanner(params)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.formData.options.hasAdd.modal = false;
              this.$Message.success('添加成功');
              this.getDatas();
            }
        })
      }
    },
  },
  components: {

  }
}
</script>
<style lang="less" scoped>
    @import './index.less';
</style>
